<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>直刷monkey测试平台</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" href="../js/lib/layui/dist/css/layui.css">
</head>
<body>
<div style="padding: 15px;" class="usereditbody layui-form-pane">
    <h1>直刷monkey测试平台</h1>
    <form class="layui-form" action="">
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend style="color:green;">基础内容：</legend>
        </fieldset>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">任务名:</label>
                <div class="layui-input-inline">
                    <input type="text" name="base_name" id="base_name" autocomplete="off" class="layui-input" >
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <span style="color:red;">* 提供英文和数组组合的名称方便后边进行操作</span>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">米账号名:</label>
                <div class="layui-input-inline">
                    <input type="text" name="mi_account_name" id="mi_account_name" autocomplete="off"
                           class="layui-input" lay-verify="required">
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <span style="color:red;">* 提供构建包的小米账号的用户名</span>
                </div>
            </div>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend style="color:green;">总体设置：</legend>
        </fieldset>
        <div class="layui-form-item">
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="checkbox" id="build" title="是否打包" checked>
                </div>
            </div>
            <div class="layui-inline">
                <div class="layui-input-inline">
                    <input type="checkbox" id="monkey" title="是否执行monkey测试" checked>
                </div>
            </div>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend style="color:green;">打包信息</legend>
        </fieldset>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">机型:</label>
                <div class="layui-input-inline">
                    <select name="device" lay-filter="device" id="device" class="layui-select" style="width: 190px;">
                        <option value="">全部</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">版本:</label>
                <div class="layui-input-inline">
                    <input type="text" name="version" id="version" placeholder="o-dev" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">地区:</label>
                <div class="layui-input-inline">
                    <input type="text" name="area" id="area" placeholder="cn" autocomplete="off" class="layui-input">
                </div>
            </div>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend style="color:green;">monkey指令设置</legend>
        </fieldset>
        <div class="layui-form-item">
            <fieldset class="layui-elem-field" style="margin-top: 30px;">
                <legend>选择执行monkey测试的包名（互联网一部）</legend>
                <div id="packages_departmentnetworkone" style="margin: 10px">
                    <!-- 包名展示  -->
                    <input type="checkbox" id="pkgindevice" name="1001" title="联系人com.android.contacts"
                           value="com.android.contacts">
                    <input type="checkbox" id="pkgindevice" name="1002" title="黄页com.miui.yellowpage"
                           value="com.miui.yellowpage">
                    <input type="checkbox" id="pkgindevice" name="1003" title="备份com.miui.backup"
                           value="com.miui.backup">
                    <!-- <input type="checkbox" id="pkgindevice" name="1004" title="一键换机" value="com.miui.backup" > -->
                    <input type="checkbox" id="pkgindevice" name="1005" title="录屏com.miui.screenrecorder"
                           value="com.miui.screenrecorder">
                    <!-- <input type="checkbox" id="pkgindevice" name="1006" title="收音机" value="com.android.contacts" > -->
                    <input type="checkbox" id="pkgindevice" name="1007" title="指南针com.miui.compass"
                           value="com.miui.compass">
                    <input type="checkbox" id="pkgindevice" name="1008" title="悬浮球com.miui.touchassistant"
                           value="com.miui.touchassistant">
                    <input type="checkbox" id="pkgindevice" name="1009" title="录音机com.android.soundrecorder"
                           value="com.android.soundrecorder">
                    <input type="checkbox" id="pkgindevice" name="1010" title="计算器com.miui.calculator"
                           value="com.miui.calculator">
                    <input type="checkbox" id="pkgindevice" name="1011" title="便签com.miui.notes" value="com.miui.notes">
                    <input type="checkbox" id="pkgindevice" name="1012" title="设置com.android.settings"
                           value="com.android.settings">
                    <!-- <input type="checkbox" id="pkgindevice" name="1013" title="联系人" value="com.android.contacts" > -->
                </div>
            </fieldset>
        </div>
        <div class="layui-form-item">
            <fieldset class="layui-elem-field" style="margin-top: 30px;">
                <legend>选择执行monkey测试的包名（选择的机型中包名）</legend>
                <div id="packages" style="margin: 10px">
                    <!-- 包名展示  -->
                    <!-- <input type="checkbox" id="pkgindevice" name="1001" title="联系人" value="com.android.contacts" >
                    <input type="checkbox" id="pkgindevice" name="1002" title="黄页" value="com.miui.yellowpage" >
                    <input type="checkbox" id="pkgindevice" name="1003" title="备份" value="com.miui.backup" >
                    <input type="checkbox" id="pkgindevice" name="1004" title="一键换机" value="com.miui.backup" >
                    <input type="checkbox" id="pkgindevice" name="1005" title="录屏" value="com.miui.screenrecorder" >
                    <input type="checkbox" id="pkgindevice" name="1006" title="收音机" value="com.android.contacts" >
                    <input type="checkbox" id="pkgindevice" name="1007" title="指南针" value="com.miui.compass" >
                    <input type="checkbox" id="pkgindevice" name="1008" title="悬浮球" value="com.miui.touchassistant" >
                    <input type="checkbox" id="pkgindevice" name="1009" title="录音机" value="com.android.soundrecorder" >
                    <input type="checkbox" id="pkgindevice" name="1010" title="计算器" value="com.miui.calculator" >
                    <input type="checkbox" id="pkgindevice" name="1011" title="便签" value="com.miui.notes" >
                    <input type="checkbox" id="pkgindevice" name="1012" title="设置" value="com.android.settings" >
                    <input type="checkbox" id="pkgindevice" name="1013" title="联系人" value="com.android.contacts" > -->
                </div>
            </fieldset>
        </div>
        <div class="layui-form-item">
            <div class="layui-inline">
                <label class="layui-form-label">指定包名:</label>
                <div class="layui-input-inline">
                    <input type="text" name="package" id="package" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">日志级别:</label>
                <div class="layui-input-inline">
                    <!--<input type="text" name="role" id="role" autocomplete="off" class="layui-input">-->
                    <select name="loglevel" id="loglevel" class="layui-select" style="width: 190px;">
                        <option value="-v">-v</option>
                        <option value="-v -v">-v -v</option>
                        <option value="-v -v -v">-v -v -v</option>
                    </select>
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">seed值:</label>
                <div class="layui-input-inline">
                    <input type="text" name="seed" id="seed" placeholder="1000" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">throttle(ms):</label>
                <!-- 用于指定用户操作（即事件）间的时延，单位是毫秒； -->
                <div class="layui-input-inline">
                    <input type="text" name="throttle" id="throttle" placeholder="500" autocomplete="off"
                           class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">事件总数:</label>
                <div class="layui-input-block">
                    <!-- 事件数 -->
                    <div class="layui-input-inline">
                        <input type="text" name="eventsnumber" id="eventsnumber" placeholder="100000" autocomplete="off"
                               class="layui-input">
                    </div>
                </div>
            </div>
        </div>
        <div class="layui-form-item">
            <fieldset class="layui-elem-field" style="margin-top: 30px;">
                <legend>每种类别事件的数目百分比，总和100</legend>
                <div style="margin: 10px">
                    <!-- 指定每种类别事件的数目百分比
                    --pct-touch ｛+百分比｝ 触摸事件的百分比
                    --pct-motion ｛+百分比｝ 动作事件的百分比
                    --pct-trackball ｛+百分比｝ 轨迹事件的百分比
                    --pct-nav ｛+百分比｝ “基本”导航事件的百分比
                    --pct-majornav ｛+百分比｝ “主要”导航事件的百分比
                    --pct-syskeys ｛+百分比｝ “系统”按键事件的百分比
                    --pct-appswitch ｛+百分比｝ 启动Activity的百分比
                    --pct-anyevent ｛+百分比｝ 其它类型事件的百分比 -->
                    <div class="layui-inline">
                        <label class="layui-form-label">触摸事件:</label>
                        <div class="layui-input-block">
                            <!-- 事件数 -->
                            <div class="layui-input-inline">
                                <input type="text" name="pct-touch" id="pct-touch" placeholder="30" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">动作事件:</label>
                        <div class="layui-input-block">
                            <!-- 事件数 -->
                            <div class="layui-input-inline">
                                <input type="text" name="pct-motion" id="pct-motion" placeholder="20" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">轨迹事件:</label>
                        <div class="layui-input-block">
                            <!-- 事件数 -->
                            <div class="layui-input-inline">
                                <input type="text" name="pct-trackball" id="pct-trackball" placeholder="0"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">“基本”导航:</label>
                        <div class="layui-input-block">
                            <!-- 事件数 -->
                            <div class="layui-input-inline">
                                <input type="text" name="pct-nav" id="pct-nav" placeholder="20" autocomplete="off"
                                       class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">“主要”导航:</label>
                        <div class="layui-input-block">
                            <!-- 事件数 -->
                            <div class="layui-input-inline">
                                <input type="text" name="pct-majornav" id="pct-majornav" placeholder="15"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">“系统”按键:</label>
                        <div class="layui-input-block">
                            <!-- 事件数 -->
                            <div class="layui-input-inline">
                                <input type="text" name="pct-syskeys" id="pct-syskeys" placeholder="0"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">启动Activity:</label>
                        <div class="layui-input-block">
                            <!-- 事件数 -->
                            <div class="layui-input-inline">
                                <input type="text" name="pct-appswitch" id="pct-appswitch" placeholder="5"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                    <div class="layui-inline">
                        <label class="layui-form-label">其它类型:</label>
                        <div class="layui-input-block">
                            <!-- 事件数 -->
                            <div class="layui-input-inline">
                                <input type="text" name="pct-anyevent" id="pct-anyevent" placeholder="10"
                                       autocomplete="off" class="layui-input">
                            </div>
                        </div>
                    </div>
                </div>
            </fieldset>
        </div>
        <div class="layui-form-item">
            <fieldset class="layui-elem-field" style="margin-top: 30px;">
                <legend>设置遇到Force、ANR和Native错误时如何处理</legend>
                <div style="margin: 10px">
                    <!-- 指定当应用程序崩溃时（Force& Close错误），Monkey是否停止运行 --ignore-crashes -->
                    <!-- 指定当应用程序发生ANR（Application No Responding）错误时，Monkey是否停止运行  --ignore-timeouts -->
                    <!-- 指定当应用程序发生许可错误时（如证书许可，网络许可等），Monkey是否停止运行  --ignore-security-exceptions -->
                    <!-- 指定当应用程序发生错误时，是否停止其运行 --kill-process-after-error -->
                    <!-- 指定是否监视并报告应用程序发生崩溃的本地代码。 --monitor-native-crashes -->
                    <input type="checkbox" id="error" name="ignore-crashes" value="--ignore-crashes"
                           title="ignore-crashes" checked>
                    <input type="checkbox" id="error" name="ignore-timeouts" value="--ignore-timeouts"
                           title="ignore-timeouts" checked>
                    <input type="checkbox" id="error" name="ignore-security-exceptions"
                           value="--ignore-security-exceptions" title="ignore-security-exceptions" checked>
                    <input type="checkbox" id="error" name="kill-process-after-error" value="--kill-process-after-error"
                           title="kill-process-after-error" checked>
                    <input type="checkbox" id="error" name="monitor-native-crashes" value="--monitor-native-crashes"
                           title="monitor-native-crashes" checked>
                </div>
            </fieldset>
        </div>
        <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
            <legend style="color:green;">执行策略</legend>
        </fieldset>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <input type="radio" id="iter" lay-filter="times" name="iter" value="0" title="每周" checked>
                <input type="radio" id="iter" lay-filter="times" name="iter" value="1" title="一次性">
            </div>
        </div>
        <div id="iterdiv" class="layui-tab-item layui-show">
            <div class="layui-form-item">
                <fieldset class="layui-elem-field" style="margin-top: 30px;">
                    <legend>选择执行的天</legend>
                    <div style="margin: 10px">
                        <!-- mon,tue,wed,thu,fri,sat,sun -->
                        <input type="checkbox" id="weekday" name="mon" value="0"
                               title="mon" checked>
                        <input type="checkbox" id="weekday" name="tue" value="1"
                               title="tue" checked>
                        <input type="checkbox" id="weekday" name="wed"
                               value="2" title="wed" checked>
                        <input type="checkbox" id="weekday" name="thu" value="3"
                               title="thu" checked>
                        <input type="checkbox" id="weekday" name="fri" value="4"
                               title="fri" checked>
                        <input type="checkbox" id="weekday" name="sat" value="5"
                               title="sat" checked>
                        <input type="checkbox" id="weekday" name="sun" value="6"
                               title="sun" checked>
                    </div>
                </fieldset>
                <div class="layui-inline">
                    <label class="layui-form-label">时间段:</label>
                    <div class="layui-input-inline">
                        <!-- <input type="text" name="begintime" id="begintime" autocomplete="off" class="layui-input"> -->
                        <input type="text" class="layui-input" id="itertime" placeholder="yyyy-MM-dd HH:mm:ss">
                    </div>
                </div>
            </div>
        </div>
        <div id="single" class="layui-tab-item">
            <div class="layui-inline">
                <label class="layui-form-label">执行时长:</label>
                <div class="layui-input-inline">
                    <input type="text" name="longtime" id="longtime" autocomplete="off" class="layui-input">
                </div>
            </div>
            <div class="layui-inline">
                <label class="layui-form-label">开始时间:</label>
                <div class="layui-input-inline">
                    <input type="text" class="layui-input" id="beginsingle" placeholder="yyyy-MM-dd HH:mm:ss">
                </div>
            </div>
        </div>
    </form>
    <fieldset class="layui-elem-field layui-field-title" style="margin-top: 20px;">
        <legend style="color: green">运行</legend>
    </fieldset>
    <div class="layui-form-item">
        <div class="layui-input-block">
            <button class="layui-btn" lay-submit="" id="submit" data-type="commit" lay-filter="demo1"
                    style="width: 200px">走起
            </button>
            <!--<button type="reset" data-type="editreset" class="layui-btn layui-btn-primary" id="editreset">重置</button>-->
        </div>
    </div>
</div>
<script src="../js/lib/jquery/dist/jquery.min.js"></script>
<script src="../js/lib/PACE/pace.min.js"></script>
<script src="../js/lib/lodash/dist/lodash.min.js"></script>
<script src="../js/lib/layui/dist/layui.js"></script>

<script>
    $().ready(function () {

    });

    baseurl = "http://10.234.197.58:8080"
    // baseurl = "http://10.234.197.102:8080"
    // baseurl = "http://0.0.0.0:8080"
    // let index = parent.layer.getFrameIndex(window.name);  //获取子窗口索引
    function get_devcies() {
        $.ajax({
            url: baseurl + "/monkey/getdevices",
            type: 'get',
            dataType: 'json',
            async: false,
            // xhrFields: {
            //     withCredentials: true
            // },
            crossDomain: true,
            success: function (res) {
                console.log(res);
                var state = new Array();
                res.devices.forEach(function (currentValue, index, arr) {
                    console.log('forEach')
                    $('#device').append('<option value="' + currentValue.sn + '">' + currentValue.product + '</option>');
                    state.push(currentValue.product.split("_")[1]);
                    // console.log(currentValue.product);
                    // $('#submit').attr("disabled","disabled")
                });
                console.log(state);
                $('#submit').attr("disabled", true);
                $('#submit').addClass("layui-btn-disabled");
                state.forEach(function (value, index, arr) {
                    if (value == "idle") {
                        $('#submit').attr("disabled", false);
                        $('#submit').removeClass("layui-btn-disabled");
                    }
                });
                // if(currentValue.product.split("_")[1] == "BUSY"){
                //         $('#submit').attr("disabled","disabled")
                // }
            }
        });
    }

    get_devcies();

    //JavaScript代码区域
    layui.use(['form', 'laydate', 'laypage', 'layer', 'table', 'carousel', 'upload', 'element'], function () {

        var form = layui.form;
        var laydate = layui.laydate;
        // form.render();
        //时间范围
        laydate.render({
            elem: '#itertime'
            , type: 'time'
            , range: true
        });
        laydate.render({
            elem: '#beginsingle'
            , type: 'datetime'
        });

        var $ = layui.$;

        // applystate.forEach(function(currentValue, index, arr){
        //     $('#status').append('<option value="' + currentValue.id + '">' + currentValue.desc + '</option>');
        // });

        var active = {
            commit: function () {
                var errhandler = "";
                $("input:checkbox[id='error']:checked").each(function (i) {
                    errhandler += $(this).val() + ";";
                });
                var pkg = "";
                $("input:checkbox[id='pkgindevice']:checked").each(function (i) {
                    pkg += $(this).val() + ";";
                });
                let build = 0;
                $("input:checkbox[id='build']:checked").each(function (i) {
                    build = 1;
                });
                let monkey = 0;
                $("input:checkbox[id='monkey']:checked").each(function (i) {
                    monkey = 1;
                });
                let execpolicy = 0;
                $("input:radio[id='iter']:checked").each(function (i) {
                    // console.log('radio');
                    // console.log($(this).val());
                    if ('0' != $(this).val()) {
                        execpolicy = $(this).val();
                    }
                });
                var weekds = "";
                $("input:checkbox[id='weekday']:checked").each(function (i) {
                    weekds += $(this).val() + ";";
                    // console.log('weekds');
                    // console.log(weekds);
                });
                // console.log($('#itertime').val());
                // console.log($('#longtime').val());
                // console.log($('#beginsingle').val());
                // var packages_departmentnetworkone = "";
                // $("input:checkbox[id='packages_departmentnetworkone']:checked").each(function(i){
                //     packages_departmentnetworkone += $(this).val() + ";";
                // });
                let param = {
                    'task_name': $('#base_name').val(),
                    'mi_account_name': $('#mi_account_name').val(),
                    'build': build,
                    'monkey': monkey,
                    'device': $('#device').val(),
                    'execpolicy': execpolicy,
                    'weekdays': weekds,
                    'iter_time': $('#itertime').val(),
                    'long_time': $('#longtime').val(),
                    'point_time': $('#beginsingle').val(),
                    'version': $('#version').val() == "" ? "o-dev" : $('#version').val(),
                    'area': $('#area').val() == "" ? "cn" : $('#area').val(),
                    // 'packages_departmentnetworkone': packages_departmentnetworkone,
                    'pkg': pkg,
                    'package': $('#package').val(),
                    'loglevel': $('#loglevel').val(),
                    'seed': $('#seed').val() == "" ? "1000" : $('#seed').val(),
                    'throttle': $('#throttle').val() == "" ? "500" : $('#throttle').val(),
                    'eventsnumber': $('#eventsnumber').val() == "" ? "100000" : $('#eventsnumber').val(),
                    'pct_touch': $('#pct-touch').val() == "" ? "30" : $('#pct-touch').val(),
                    'pct_motion': $('#pct-motion').val() == "" ? "20" : $('#pct-motion').val(),
                    'pct_trackball': $('#pct-trackball').val() == "" ? "0" : $('#pct-trackball').val(),
                    'pct_nav': $('#pct-nav').val() == "" ? "20" : $('#pct-nav').val(),
                    'pct_majornav': $('#pct-majornav').val() == "" ? "15" : $('#pct-majornav').val(),
                    'pct_syskeys': $('#pct-syskeys').val() == "" ? "0" : $('#pct-syskeys').val(),
                    'pct_appswitch': $('#pct-appswitch').val() == "" ? "5" : $('#pct-appswitch').val(),
                    'pct_anyevent': $('#pct-anyevent').val() == "" ? "10" : $('#pct-anyevent').val(),
                    'errhandler': errhandler,
                };
                console.log(param);
                $.ajax({
                    url: baseurl + "/monkey/start",
                    type: 'post',
                    dataType: 'json',
                    data: JSON.stringify(param),
                    async: true,
                    // xhrFields: {
                    //     withCredentials: true
                    // },
                    crossDomain: true,
                    success: function (res) {
                        $('#submit').attr("disabled", true);
                        $('#submit').addClass("layui-btn-disabled");
                    }
                });
            }
        };

        $('#submit').on('click', function () {
            console.log("commit");
            var type = $(this).data('type');
            console.log(type);
            active[type] ? active[type].call(this) : '';
        });

        form.on('radio(times)', function (data) {
            // console.log(data.elem); //得到radio原始DOM对象
            // console.log(data.value); //被点击的radio的value值
            if ('1' == data.value) {
                $('#iterdiv').removeClass("layui-show");
                $('#single').addClass("layui-show");
            } else {
                $('#single').removeClass("layui-show");
                $('#iterdiv').addClass("layui-show");
            }
        });

        form.on('select(device)', function (data) {
            console.log("device select");
            console.log(data.value);
            $.ajax({
                url: baseurl + "/monkey/getpackagesindevice?device=" + data.value,
                type: 'get',
                dataType: 'json',
                async: false,
                // xhrFields: {
                //     withCredentials: true
                // },
                crossDomain: true,
                success: function (res) {
                    $("#packages").html("");
                    console.log(res);
                    $.each(res.packages, function (key, val) {
                        if (val.value != " usage") {
                            var input = '<input type="checkbox" id="pkgindevice" name="' + val.key + '" title="' + val.value + '" value="' + val.value + '" >'
                            console.log(input);
                            $("#packages").append(input);
                        }
                    });
                    form.render();
                }
            });
        });

        form.verify({
            requried: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[a-zA-Z0-9_\u4e00-\u9fa5\\s·]+$").test(value)){
                    return '输入标红字段...';
                }
            }
        });
    });






















</script>
</body>
</html>
